package com.usian.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Maps;
import com.usian.admin.mapper.LoginMapper;
import com.usian.admin.service.LoginService;
import com.usian.model.admin.dtos.AdUserDto;
import com.usian.model.admin.pojos.AdUser;
import com.usian.model.base.BaseServiceImpl;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.utils.common.AppJwtUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.util.HashMap;

@Service
@Transactional
public class LoginServiceImpl extends BaseServiceImpl<LoginMapper, AdUser> implements LoginService {
    @Override
    public ResponseResult login(AdUserDto dto) {
        //1.判空校验 用户名，密码不能为空
        if (dto == null || StringUtils.isEmpty(dto.getName()) || StringUtils.isEmpty(dto.getPassword())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "用户名或密码不能为空");
        }
        //2.根据用户名去查询
        LambdaQueryWrapper<AdUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AdUser::getName,dto.getName());
        AdUser user = this.getOne(wrapper);
        //3.如果查出来继续判断，如果没查出来则直接返回用户名不存在
        if (user==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_NamePASSWORD_ERROR);
        }
        //4.如果有用户将查询出来的密码加盐甲米
        String passwordSalt=dto.getPassword()+user.getSalt();//将输入的密码加盐salt
        String md5password=DigestUtils.md5DigestAsHex(passwordSalt.getBytes());//将密码家密
        //5然后将加盐加密之后的密码跟数据库对比，相同则登录成功，否则登录失败
        if (md5password.equals(user.getPassword())){
            //登录成功  传给前台
            //记录登录状态，生成token jwt
            HashMap<String, Object> map = Maps.newHashMap();
            //生成一个 jwt
            String token = AppJwtUtil.getToken(user.getId().longValue());
            map.put("token",token);
            //将密码和盐清空
            user.setPassword("");
            user.setSalt("");
            map.put("user",user);
            return ResponseResult.okResult(map);
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_NamePASSWORD_ERROR);

//        Wrapper wrapper=  new QueryWrapper<AdUser>();
//        ((QueryWrapper)wrapper).eq("name",dto.getName());
//        List<AdUser> list = list(wrapper);
//        if (list!=null&&list.size()==1){
//            AdUser adUser = list.get(0);
//          String pswd= DigestUtils.md5DigestAsHex((dto.getPassword()+adUser.getSalt()).getBytes());
//          if (adUser.getPassword().equals(pswd)){
//              HashMap<String, Object> map = Maps.newHashMap();
//              adUser.setPassword("");
//              adUser.setSalt("");
//              map.put("token", AppJwtUtil.getToken(adUser.getId().longValue()));
//              map.put("user",adUser);
//              return ResponseResult.okResult(map);
//          }else {
//              return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
//          }
//        }else {
//            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "用户不存在");
//        }
//
//    }

    }
}
